#include "System.h"
#include "MainWidget.h"
#include "LoginDialog.h"

#include <iostream>
#include <QtSql>
using namespace std;

System::System()
{
    createDatabase();
    createTable();
}

void System::createDatabase()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("./information.db");

    if(!db.open()) {
        //QMessageBox::critical(this, "tip", "创建数据库失败");
        cout<<"数据库创建失败"<<endl;
    } else {
        cout<<"数据库创建成功"<<endl;
    }
}

void System::createTable()
{
    QString q = "CREATE TABLE IF NOT EXISTS account (account TEXT PRIMARY KEY NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL, name1 TEXT NOT NULL, id1 TEXT NOT NULL, phone1 TEXT NOT NULL, name2 TEXT, id2 TEXT, phone2 TEXT)";
    QSqlQuery query(db);
    if (query.exec(q)) {
        cout<<"账户Table创建成功"<<endl;
    } else {
        cout<<"账户Table创建失败"<<endl;
    }
}

void System::registerAccount1(QString account, QString password, QString email, QString name1, QString id1, QString phone1)
{
    QSqlQuery query(db);
    QString s = "INSERT INTO account(account, password, email, name1, id1, phone1) VALUES (?, ?, ?, ?, ?, ?)";
    query.prepare(s);
    query.bindValue(0, account);
    query.bindValue(1, password);
    query.bindValue(2, email);
    query.bindValue(3, name1);
    query.bindValue(4, id1);
    query.bindValue(5, phone1);

    if (!query.exec()) {
        cout<<"创建单名账号失败"<<endl;
    } else {
        cout<<"创建单名账号成功"<<endl;
    }
}

void System::registerAccount2(QString account, QString password, QString email, QString name1, QString id1, QString phone1, QString name2, QString id2, QString phone2)
{
    QSqlQuery query(db);
    QString s = "INSERT INTO account(account, password, email, name1, id1, phone1, name2, id2, phone2) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
    query.prepare(s);
    query.bindValue(0, account);
    query.bindValue(1, password);
    query.bindValue(2, email);
    query.bindValue(3, name1);
    query.bindValue(4, id1);
    query.bindValue(5, phone1);
    query.bindValue(6, name2);
    query.bindValue(7, id2);
    query.bindValue(8, phone2);

    if (!query.exec()) {
        cout<<"创建联名账号失败"<<endl;
    } else {
        cout<<"创建联名账号成功"<<endl;
    }
}
